<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="需求计划序号" prop="orderId">
      <el-input v-model="dataForm.orderId" placeholder="需求计划序号"></el-input>
    </el-form-item>
    <el-form-item label="采购计划序号" prop="stockId">
      <el-input v-model="dataForm.stockId" placeholder="采购计划序号"></el-input>
    </el-form-item>
    <el-form-item label="询价书序号" prop="enquireId">
      <el-input v-model="dataForm.enquireId" placeholder="询价书序号"></el-input>
    </el-form-item>
    <el-form-item label="合同申请序号" prop="contAppId">
      <el-input v-model="dataForm.contAppId" placeholder="合同申请序号"></el-input>
    </el-form-item>
    <el-form-item label="报价书序号" prop="quoteId">
      <el-input v-model="dataForm.quoteId" placeholder="报价书序号"></el-input>
    </el-form-item>
    <el-form-item label="合同序号" prop="contId">
      <el-input v-model="dataForm.contId" placeholder="合同序号"></el-input>
    </el-form-item>
    <el-form-item label="采购状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="采购状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          orderId: '',
          stockId: '',
          enquireId: '',
          contAppId: '',
          quoteId: '',
          contId: '',
          status: ''
        },
        dataRule: {
          orderId: [
            { required: true, message: '需求计划序号不能为空', trigger: 'blur' }
          ],
          stockId: [
            { required: true, message: '采购计划序号不能为空', trigger: 'blur' }
          ],
          enquireId: [
            { required: true, message: '询价书序号不能为空', trigger: 'blur' }
          ],
          contAppId: [
            { required: true, message: '合同申请序号不能为空', trigger: 'blur' }
          ],
          quoteId: [
            { required: true, message: '报价书序号不能为空', trigger: 'blur' }
          ],
          contId: [
            { required: true, message: '合同序号不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '采购状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/purchase/idmapping/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 200) {
                this.dataForm.orderId = data.idMapping.orderId
                this.dataForm.stockId = data.idMapping.stockId
                this.dataForm.enquireId = data.idMapping.enquireId
                this.dataForm.contAppId = data.idMapping.contAppId
                this.dataForm.quoteId = data.idMapping.quoteId
                this.dataForm.contId = data.idMapping.contId
                this.dataForm.status = data.idMapping.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/purchase/idmapping/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'orderId': this.dataForm.orderId,
                'stockId': this.dataForm.stockId,
                'enquireId': this.dataForm.enquireId,
                'contAppId': this.dataForm.contAppId,
                'quoteId': this.dataForm.quoteId,
                'contId': this.dataForm.contId,
                'status': this.dataForm.status
              })
            }).then((data) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
